<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <div style="margin-top: 10px">
<!--        <el-button class="filter-item" size="mini" type="primary" icon="el-icon-zoom-in" @click="applyCourse">申请课程-->
<!--        </el-button>-->
      </div>F
    </div>
    <!--表单组件-->
    <e-from ref="form" :is-add="isAdd" @close="load()"/>
    <!--学生信息组件-->
    <student-info ref="studentForm"/>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="loading"
      :data="data"
      size="small"
      style="width: 100%;"
      stripe
      empty-text="暂无数据"
      highlight-current-row
      current-row-key="id"
      @select="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column prop="name" label="课程名称" min-width="200"/>
      <el-table-column prop="classroom" label="上课教室" align="center"/>
      <el-table-column prop="tname" label="上课教师" align="center"/>
      <el-table-column prop="totalPeople" label="人数" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column prop="sname" label="上课时间" show-overflow-tooltip min-width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.sw}}&nbsp; {{scope.row.sse}}
        </template>
      </el-table-column>
      <el-table-column prop="wname" label="上课周数" show-overflow-tooltip min-width="120" align="center"/>
      <el-table-column prop="status" label="操作" width="300" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button size="mini"
                     type="primary" @click="agree(scope.row.id)">同意
          </el-button>
          <el-button size="mini"
                     type="info" @click="edit(scope.row)">修改
          </el-button>
          <el-button size="mini"
                     type="warning" @click="back(scope.row.id)">退回
          </el-button>
        </template>

      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-row>
      <el-col :offset="10">
        <el-pagination
          :total="total"
          :current-page="params.offset"
          style="margin-top: 8px;"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes"
          @size-change="sizeChange"
          @current-change="pageChange"/>
      </el-col>
    </el-row>
    <apply-form ref="form" :is-add="isAdd" @close="load"></apply-form>
  </div>
</template>

<script>
  import service from '../../../utils/request'
  import applyForm from './applyForm'
  import course from '@/api/admin/course/course'

  export default {
    components: {
      applyForm,
    },
    data() {
      return {
        exportUrl: 'http://localhost:8080/api/export',
        fileList: [],
        loading: true,
        total: 0,
        show: false,
        isAdd: true,
        team: [],
        week: [],
        section: [],
        courseId: null,
        params: {
          offset: 1,
          limit: 10,
          keyword: null,
          teamId: null,
          weekId: null,
          sectionId: null,
          status: null
        },
        data: null
      }
    },
    created() {
      this.load()
    },
    methods: {
      subDelete() {

      },
      load() {
        course.findApply(this.params).then(res => {
          this.data = res.records
          this.loading = false
          this.currentPage = res.current
          this.total = res.realTotal
        }).catch(error => {
          this.loading = false
          this.$notify({
            title: '错误信息',
            message: '登录超时，请重新登录'
          })
          this.$router.push({path: '/login'})
        })
      },
      sizeChange(val) {
        this.params.limit = val
        this.load()
      },
      pageChange(val) {
        this.params.offset = val
        this.load()
      },
      add() {
        this.isAdd = true
        this.$refs.form.dialog = true
      },
      edit(data) {
        this.isAdd = false
        const _this = this.$refs.form
        _this.form = data
        _this.dialog = true
      },
      toQuery() {
        this.load()
      },
      selectSection(val) {
      },
      StudentInfo() {
        this.$refs.studentForm.dialog = true
      },
      handleSelectionChange(val, row) {
        if (val.length == 0) {
          this.courseId = null
        } else if (val.length > 1) {
          this.$refs.table.clearSelection()
          this.$refs.table.toggleRowSelection(row)
          val.splice(0, val.length - 1)
          this.courseId = val[0].id
        } else {
          this.courseId = val[0].id
        }
      },
      selectStudent() {
        if (this.courseId == null) {
          this.$message({
            type: 'error',
            message: '请先选择一门课程'
          })
        } else {
          this.$refs.studentForm.dialog = true
          this.$refs.studentForm.params.id = this.courseId
          this.$refs.studentForm.load()
        }
      },
      applyCourse() {
        this.$refs.form.dialog = true
      },
      agree(val) {
        course.teacherCourseAgree(val).then(res => {
          this.load()
          if (res) {
            this.$message({
              type: 'success',
              message: '成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: '失败'
            })
          }
        })
      },
      back(val) {
        course.teacherCourseBack(val).then(res => {
          this.load()
          if (res) {
            this.$message({
              type: 'success',
              message: '成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: '失败'
            })
          }
        })
      },
      subDelete(val) {
        course.teacherCourseDelete(val).then(res => {
          if (res) {
            this.$message({
              type: 'success',
              message: '成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: '失败'
            })
          }
        })
      },
      switchText(val) {
        if (val == 0) {
          return "等待审核";
        } else if (val == 1) {
          return "审核通过";
        } else {
          return "退回";
        }
      }
    }
  }
</script>

<style scoped>
  .status {
    color: green;
  }
</style>
